/**
* @module analytics
* @author: Hjava
* @since: 2018-03-02 10:33:59
*/

Object.defineProperty(exports, "__esModule", { value: true });
var deviceInfo_1 = __webpack_require__(/*! _util/deviceInfo */ "./src/core/utils/deviceInfo.ts");
var typeChecker_1 = __webpack_require__(/*! _util/typeChecker */ "./src/core/utils/typeChecker.ts");
var userData_1 = __webpack_require__(/*! _util/userData */ "./src/core/utils/userData.ts");
var uuid_1 = __webpack_require__(/*! _util/uuid */ "./src/core/utils/uuid.ts");
var log_1 = __webpack_require__(/*! ./../utils/log */ "./src/core/utils/log.ts");
var localData_1 = __webpack_require__(/*! _util/localData */ "./src/core/utils/localData.ts");
var LogManager = /** @class */ (function () {
    function LogManager() {
        this._analytics = null;
        this._extendObject = null;
    }
    LogManager.prototype.getInitReportUid = function () {
        try {
            var uid = userData_1.default.get('uid') || '0';
            if (!uid || uid == '0') {
                uid = localData_1.default.get('last_login_success_uid') || '0';
            }
            return uid;
        }
        catch (e) {
            log_1.default.error(e);
            return '0';
        }
    };
    LogManager.prototype.init = function (extendObject) {
        var Analytics;
        if (true) {
            this._extendObject = extendObject;
            Analytics = __webpack_require__(/*! @xm/analytics */ "./node_modules/@xm/analytics/dist/index.js");
            this._analytics = new Analytics('xm_sdk_report', {
                ai: userData_1.default.get('appId'),
                pt: this._getPT(),
                uid: this.getInitReportUid(),
                sv: "4.22.144",
                env: userData_1.default.get('env'),
                av: userData_1.default.get('av'),
                did: userData_1.default.get('deviceId'),
                traceid: userData_1.default.get('traceid') || uuid_1.default.next()
            }, {
                serviceid: 4,
                comboSize: 10,
                forceSendInterval: 10 * 1000,
                maxSendInterval: 5 * 1000 //发送间隔至少5秒以上
            });
        }
    };
    LogManager.prototype.reportLogPrint = function (data) {
        try {
            if (!data) {
                return;
            }
            log_1.default.log('LogManager::reportLogPrint info:' + JSON.stringify(data));
        }
        catch (error) {
            log_1.default.error('LogManager::reportLogPrint error info:' + error);
        }
    };
    /**
     * 判断规则见下面注释，逻辑见此图：https://file.neixin.cn/pan/im/2/image/AumQ8_YfdTkKoM0HWPKyVp4UfnasO431QbvGZKQn-qxeK67A5ftv_dCKQ-wGqz7_Ew?filename=export.png
     */
    LogManager.prototype.report = function () {
        var params = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            params[_i] = arguments[_i];
        }
        if (!this._analytics) {
            return;
        }
        if (this._extendObject && this._extendObject.report) {
            // 调用外部对象的上报能力
            this._extendObject.report(params);
        }
        // let args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments));
        var args = (params.length === 1 ? [params[0]] : params);
        // 长度为0即提示不支持
        if (args.length === 0) {
            log_1.default.error('analytics param must be 1 at least');
            return;
        }
        // 长度为3的肯定是分组上报
        if (args.length === 3) {
            this._sendLogByGroup.apply(this, args);
            return;
        }
        if (args.length === 2) {
            // 两个参数都为string的也是分组上报
            if (typeChecker_1.default.isString(args[0]) && typeChecker_1.default.isString(args[1])) {
                this._sendLogByGroup.apply(this, args);
                return;
            }
            // 第一个参数为object时，第二个参数也一定是object，是单条上报
            if (typeChecker_1.default.isObject(args[0])) {
                this._sendLog.apply(this, args);
                return;
            }
            // 第一个参数为string，第二个参数为object时，需要看第二个参数是否有nm字段来判断是否为分组上报
            if (typeChecker_1.default.isString(args[0]) && typeChecker_1.default.isObject(args[1])) {
                if (args[1].nm) {
                    this._sendLogByGroup.apply(this, args);
                    return;
                }
                else {
                    this._sendLog.apply(this, args);
                    return;
                }
            }
        }
        // 只有一个参数时，一定是单条上报
        if (args.length === 1) {
            this._sendLog.apply(this, args);
            this.reportLogPrint(args);
            return;
        }
    };
    LogManager.prototype.setAnalyticsUid = function (uid) {
        if (this._analytics) {
            this._analytics.set({ uid: uid });
        }
    };
    /**
     * 发送单条日志ti
     * @param {string|object} data 日志数据
     * @param {any} options 日志上报选项
     */
    LogManager.prototype._sendLog = function (data, options) {
        if (this._analytics) {
            try {
                this._analytics.report(data, options);
            }
            catch (e) {
                log_1.default.error(data, options);
            }
        }
    };
    LogManager.prototype._sendLogByGroup = function (label, data, options) {
        if (this._analytics) {
            try {
                this._analytics.groupReport(label, data, options);
            }
            catch (e) {
                log_1.default.error(data, options);
            }
        }
    };
    /**
     * 发送组日志
     * @param {string} label 组名
     * @param {string|object} data 日志数据
     * @param {object} options 日志上报选项
     */
    LogManager.prototype._getPT = function () {
        switch (deviceInfo_1.default.deviceType) {
            case 2: {
                return 'web';
            }
            case 4: {
                return 'pc';
            }
        }
    };
    return LogManager;
}());
exports.default = new LogManager();


//# sourceURL=webpack://MTDX/./src/core/utils/analytics.ts?